package entity;

import pers.cz.excel.PoiUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: PostGirl-panent
 * @description: Main
 * @author: Cheng Zhi
 * @create: 2021-02-28 11:37
 **/
public class Main {

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Test entityTest = new Test();
        entityTest.setAddress("Shanghai");
        entityTest.setAge(18);
        entityTest.setName("cz");
/*        String[] fields = getFieldName(entityTest);
        Gson json = new Gson();
        System.out.println(json.toJson(getFieldName(entityTest)));*/

        List<Test> list = new ArrayList<Test>();
        list.add(entityTest);
        PoiUtils.exportToExcel(list);
    }

    public static <T> void test1(T t) {

        //getFieldName(t);
    }

    /**
     * 获取对象属性值
     * @param o
     * @return
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    private static String[] getFieldName(Object o) {
        Field[] fields=o.getClass().getDeclaredFields();
        String[] fieldNames=new String[fields.length];
        String[] fieldValues = new String[fieldNames.length];
        for(int i=0;i<fields.length;i++){
            fieldNames[i]=fields[i].getName();
        }
        try {
            for (int i=0; i<fieldNames.length; i++) {
                String fieldName = fieldNames[i];
                String field = o.getClass().getMethod("get" + returnFirstCapital(fieldName)).invoke(o).toString();
                fieldValues[i] = field;
            }
        } catch(Exception e) {

        }
        return fieldValues;
    }

    /**
     * 判断字符串首字母是否为大写，如果不是转化为大写
     * @param str
     * @return
     */
    public static String returnFirstCapital(String str) {
        if (str.charAt(0) >= 'A' && str.charAt(0) <= 'Z') {
            return str;
        }
        char[] ch = str.toCharArray();
        ch[0] -= 32;
        return String.valueOf(ch);
    }

    /**
     * 判断字符串首字母是否为大写，如果不是转化为大写
     * @param str
     * @return
     */
    public static Boolean isBig(String str) {

        if (str.charAt(0) >= 'A' && str.charAt(0) <= 'Z') {
            return true;
        }
        return false;

    }
}
